package com.blockmeta.bbs.businesslibrary.widget.chartview.Layer;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.text.TextUtils;


import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.util.Format2Util;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.Coordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.DailyCoordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.ChartData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 因为要放在分时图里，数据集要根据session进行变换，所以在barlayer的基础上新建了dailybar
 */
class DailyBarLayer extends BasicLayer
{
	protected final double m_dbNone = Double.MIN_VALUE;

	public DailyBarLayer()
	{
		super();
	}

	@Override
	public Layer.ChartTypeEnum getLayerTypeEnum()
	{
		return Layer.ChartTypeEnum.DAILYBAR;
	}

	@Override
	public Coordinate createCoordinate()
	{
		return new DailyCoordinate();
	}

	public List<CandleData> convertDate(List<CandleData> pList)
	{
		if (pList == null)
		{
			return null;
		}

		int tListSize = pList.size();
		List<Integer> tSession = ((DailyCoordinate)m_callback.getCoordinate()).getSession();

		List<CandleData> tList = new ArrayList<CandleData>();

		int tTempIndex = -1;
		for (int ii = 0; ii < tListSize; ii++)
		{
			long tTime = pList.get(ii).m_nDate;
			int tIndex = tSession.indexOf(tTime);
			if (tIndex > tTempIndex)
			{
				for (int jj = tTempIndex + 1; jj < tIndex; jj++)
				{
					tList.add(new CandleData(0, tSession.get(jj), pList.get(ii).getDbClose(), 0,pList.get(ii).m_dbAvgPx, 0));
				}
				tTempIndex = tIndex;
				tList.add(pList.get(ii).clone());

				if (tTempIndex == -1)
				{
					((DailyCoordinate)m_callback.getCoordinate()).setStartIndex(tList.size() - 1);
				}
			}
		}

		if (tList.size() > 0)
		{
			tList.get(tList.size() - 1).m_bDrawDot = true;
		}

		return tList;
	}

	@Override
	public void onDrawLayer(Canvas pCanvas, Paint pPaint, Point pPoint)
	{
		Coordinate tCoordinate = m_callback.getCoordinate();
		List<CandleData> tDataList = convertDate(m_callback.getCandleDataList());

		RectF layer = tCoordinate.getLayerRectF();

		// Bar 线的上分界线
//		pPaint.setColor(0xFFD3D8DE);
//		pPaint.setStrokeWidth(DimentionsUtil.dp2px(1));
//		pCanvas.drawLine(layer.left, layer.top, layer.right, layer.top, pPaint);

		String tCloseText = null;
		String tOpenText = null;
		String tHighText = null;
		String tLowText = null;
		float tValueText = 0;
		float tVolumnText = 0;
		String tAvgText = null;
		int tXPoint = pPoint.x;
		CandleData tTimeCandle = null;

		if (tDataList != null && tDataList.size() > 0)
		{
			int tListSize = tDataList.size();
			int tStartIndex = tCoordinate.getStartIndex();
			float tXFirstPosition = tCoordinate.getXFirstPosition();
			float tXInterval = tCoordinate.getXInterval();
			float tYFirstposition = tCoordinate.getYFirstPosition();
			
			float tYMaxValue = tCoordinate.getMaxValue();
			float tYMinValue = 0;//tCoordinate.getMinValue();
			float tYValueDist = tYMaxValue - tYMinValue;
			float tYValueLength = tCoordinate.getValueHeight();

			float tCurrentYPosition = 0.f;
			float tTempYPosition = 0.f;
			float tCurrentXPosition = 0.f;
			float tTempXPosition = 0.f;
			pPaint.setAntiAlias(true);
			pPaint.setColor(m_callback.getColor());
			pPaint.setStrokeWidth(2f);

			float tBarWidth = Math.min(tXInterval / 3, 5);

			int tSysUpColor = m_callback.getSysUpColor();
			int tSysDownColor = m_callback.getSysDownColor();
			int tSysEvenColor = m_callback.getSysEvenColor();

			for (int i = tStartIndex; i < tListSize; i++)
			{
				//如果收盘价为空就跳过
				if (tDataList.get(i).m_lVolume == m_dbNone)
				{
					continue;
				}


				float tVolumn = tDataList.get(i).m_lVolume;
				float tOpen = (float) tDataList.get(i).getDbOpen();
				float tClose = (float) tDataList.get(i).getDbClose();
				
				tTempXPosition = tCurrentXPosition;
				tCurrentXPosition = tXFirstPosition + tXInterval * (i - tStartIndex);
//				tTempYPosition = tCurrentYPosition;

				tCurrentYPosition = tYFirstposition - ((tVolumn - tYMinValue) * tYValueLength / tYValueDist);
//				float tOpenYPosition = tYFirstposition - ((tOpen - tYMinValue) * tYValueLength / tYValueDist);
//				float tCloseYPosition = tYFirstposition - ((tClose - tYMinValue) * tYValueLength / tYValueDist);


				if (((tCurrentXPosition - tXInterval / 2) <= pPoint.x && pPoint.x < (tCurrentXPosition + tXInterval / 2)) || ((i == tListSize - 1) && pPoint.x > tCurrentXPosition + tXInterval / 2) || (i == tStartIndex && pPoint.x < tCurrentXPosition - tXInterval / 2))
				{
					float tValueaa = (tYFirstposition - pPoint.y) * tYValueDist / tYValueLength + tYMinValue;
					tValueText = tValueaa;
					ChartData.ChartPeroid tChartType = m_callback.getChartType();

					tVolumnText = tDataList.get(i).m_lVolume;
					tCloseText = String.valueOf((float) tDataList.get(i).getDbClose());
					tOpenText = String.valueOf((float) tDataList.get(i).getDbOpen());
					tHighText = String.valueOf((float) tDataList.get(i).getDbHigh());
					tLowText = String.valueOf((float) tDataList.get(i).getDbLow());
					tAvgText = String.valueOf((float) tDataList.get(i).m_dbAvgPx);

					tTimeCandle = tDataList.get(i);
					tXPoint = (int) tCurrentXPosition;
				}

				double open = 0;

				if (i == 0)
				{
					open = getYesterClose();
				}
				else
				{
					open = tDataList.get(i - 1).getDbClose();
				}
				
				if (open > tClose)
				{
					pPaint.setColor(tSysDownColor);
				}
				else
				{
					pPaint.setColor(tSysUpColor);
				}

				pCanvas.drawRect(tCurrentXPosition - tBarWidth, tCurrentYPosition, tCurrentXPosition + tBarWidth, tYFirstposition, pPaint);
			}
		}

		ChartData.ChartPeroid tChartType = m_callback.getChartType();

		String time = tTimeCandle == null ? "" : getDisplayX(tChartType, tTimeCandle);
		String value = FormatUtil.longToVolumeHumanReadableFormat((long) tValueText, 2);

		RectF tLayerRect = tCoordinate.getLayerRectF();
		setCrossData(pCanvas, pPaint, time, value, new Point(tXPoint, pPoint.y));
		if (tLayerRect.contains(tXPoint, pPoint.y))
		{


			if (m_callback != null)
			{
				int tDigit = Math.max(tCoordinate.getDigit(), 1);

				tCloseText = getDigitString(tCloseText, tDigit);
				tOpenText = getDigitString(tOpenText, tDigit);
				tHighText = getDigitString(tHighText, tDigit);
				tLowText = getDigitString(tLowText, tDigit);
				tAvgText = getDigitString(tAvgText, tDigit);

				tAvgText = tAvgText == null ? "" : tAvgText;
				HashMap<String, String> tMap = new HashMap<String, String>();
				tMap.put("Close", tCloseText);
				tMap.put("Open", tOpenText);
				tMap.put("High", tHighText);
				tMap.put("Low", tLowText);
				tMap.put("Volumn", tVolumnText == 0 ? "" : Format2Util.doubleToHumanReadableFormat(tVolumnText, 2));
				tMap.put("Avg",tAvgText);
				tMap.put("Time",time);
				m_callback.pointFeedback(tMap,pPoint );
			}
		}
	}

	private String getDigitString(String value, int digit)
	{
		if (! TextUtils.isEmpty(value))
		{
			try
			{
				value = FormatUtil.getFormatString(Double.parseDouble(value), digit);
			}
			catch (Exception e)
			{
			}
		}
		else
		{
			value = "";
		}

		return value;
	}

	@Override
	public void drawYesterClose(Canvas pCanvas, Paint pPaint)
	{
	}

}
